home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Development Platforms / Apple II / DTS Apple II Sample Code / IR 2.0 / Source Code / IR Module / IRModule.p < prev    next >
Encoding:
Text File  |  1992-02-25  |  14.5 KB  |  441 lines  |  [TEXT/MPS ]

  1. {
  2.     IR module in MPW IIgs Pascal
  3.  
  4.     Matt Deatherage, 12/11/91
  5.  
  6.     Based on:
  7.     Sample Nifty List module in MPW IIgs Pascal
  8.     4-Dec-91 Dave Lyons
  9.  
  10.    Revision history:
  11.  
  12.    2.0a3       Matt Deatherage      12/12/91
  13.  
  14.    First release of file.  All string types of odd length due to a strange bug
  15.    in PLib 1.1a27 that makes string copying of strings of odd length (including
  16.    length bytes) fail if the strings cross a bank boundary.  So, to be safe, all
  17.    string types are of odd length so they'll take an even number of bytes with
  18.    the length byte.
  19.  
  20.    2.0a4       Matt Deatherage      12/18/91
  21.  
  22.    cmdIRInstall was checking the low bit of OS_KIND instead of checking for
  23.    a value of $01.  It's now also reading a byte instead of a word.  Fixed.
  24.  
  25.    Locator.p updated so SendRequest declares target and dataIn as UNIV longint
  26.    and dataOut as ptr.  Calls to SendRequest accordingly changed.
  27.  
  28.    cmdKillIR didn't actually remember to remove IR's request procedure before
  29.    it called UserShutDown.  Now it does.
  30.  
  31.    Still no check for stack space -- trying to use some of these commands from
  32.    P8 may overflow the stack and cause real problems.
  33.  
  34.    Library bug with even-length strings fixed, but the types are still the
  35.    way they were in 2.0a3 because it's not hurting anything.
  36.  
  37.    2.0b1       Matt Deatherage      02/18/92
  38.  
  39.    cmdKillIR was fixed in 2.0a4 to remove the request procedure; now it does it
  40.    by user ID instead of name since IR now appends the user ID to the name
  41.    string.
  42.  
  43.    2.0b2       Matt Deatherage      02/20/92
  44.  
  45.    cmdGetIRPrefs now knows about the new irIgnoreProblems preference.
  46.  
  47.    2.0b3       Matt Deatherage      02/22/92
  48.  
  49.    Eliminated message on entry to Nifty List (to be nice to Dave).  Changed
  50.    cmdIRInstall to correctly return the Tool Locator error if IR didn't
  51.    change the result code in the buffer.
  52.     
  53.     2.0f1            Matt Deatherage        02/24/92
  54.     
  55.     Added resource fork for version and two comment resources.  Open it and
  56.     read what we need in actBirth; we don't use it anywhere else.  Before
  57.     calling VersionString to get the version in ASCII form, we check for
  58.     QDVersion 3.7 or greater to make sure we're running 6.0.  cmdIRInfo
  59.     now displays that version number as well.  I even (eventually) remembered
  60.     to save and restore the current resource application so if someone installs
  61.     Nifty List from within an application I don't kill the app's resource fork.
  62.     
  63.     2.0            Matt Deatherage        02/25/92
  64.     
  65.     First release to the world.  No code changes from 2.0f1.
  66.  
  67. }
  68.  
  69. {[j=12-/32/80!,o=95,a-]}       {PasMat options}
  70.  
  71. UNIT IRModule;
  72.  
  73.    INTERFACE
  74.  
  75.       USES Types,Memory,Locator,QuickDraw,Misctool,GSOS,Loader,Resources,GSBug,NiftyList,IR;
  76.  
  77.          {$Z+}                 { Export these procedures and functions. These are in the
  78.                                 assembly file. }
  79.  
  80.       FUNCTION NDAOpen: ptr;
  81.  
  82.       PROCEDURE NDAAction(Code: Integer; Param: Longint);
  83.       {$Z-}
  84.  
  85.    IMPLEMENTATION
  86.  
  87.       CONST
  88.          numCommandsPlusTwo = 8; { first one is special }
  89.  
  90.       TYPE
  91.          commandTableType = ARRAY [1..numCommandsPlusTwo] OF
  92.                                RECORD
  93.                                   cmdName: ptr; { to Pascal string }
  94.                                   cmdProc: procPtr;
  95.                                   helpProc: procPtr;
  96.                                END;
  97.  
  98.          Str127      = String[127];
  99.  
  100.       VAR
  101.          InfoRec    : ModuleInfoType;
  102.          myCommandTable: commandTableType;
  103.          TestText   : String[39];
  104.          d          : Longint; { dummy }
  105.          tempString : Str255;
  106.          moduleName,commandKillIR,commandIRInfo,commandGetIRPrefs,commandSetIRPrefs,
  107.          commandDoIRPrefs,commandIRInstall: String[31];
  108.  
  109.          myEvalBuffer: EvalExprBuffer;
  110.          myRangeBuffer: GetRangeBuffer;
  111.          GFIBuffer  : FileInfoRecGS;
  112.          IRString,ModuleVersion: String32;
  113.  
  114.       FUNCTION NLService(Param: UNIV Longint; Code: Integer): Longint;
  115.  
  116.          BEGIN
  117.     { dummy function -- first four bytes get patched at runtime to
  118.                         jump into Nifty List }
  119.          END;
  120.  
  121.       PROCEDURE WriteCR;
  122.  
  123.          BEGIN
  124.             d := NLService(NIL,nlCrout);
  125.          END;
  126.  
  127.         PROCEDURE DisplayLineNoCR(theText: Str127);
  128.         
  129.             BEGIN
  130.                 d := NLService(@theText,nlWriteStr);
  131.             END;
  132.  
  133.       PROCEDURE DisplayLine(theText: Str127);
  134.  
  135.          BEGIN
  136.             d := NLService(@theText,nlWriteStr);
  137.             WriteCR;
  138.          END;
  139.  
  140.       PROCEDURE DisplayErrorLine(theText: Str127; theError: Integer);
  141.  
  142.          VAR
  143.             temp       : Longint;
  144.  
  145.          BEGIN
  146.             d := NLService(@theText,nlWriteStr);
  147.             temp := theError;
  148.             d := NLService(temp,nlWriteWord);
  149.             WriteCR;
  150.          END;
  151.  
  152.       PROCEDURE helpModule;
  153.  
  154.          BEGIN
  155.             DisplayLineNoCR('IR Module ');
  156.                 DisplayLineNoCR(ModuleVersion);
  157.                 DisplayLine(':');
  158.             DisplayLine('  \killir      \irinfo      \getirprefs  \setirprefs');
  159.             DisplayLine('  \doirprefs   \irinstall');
  160.          END;
  161.  
  162.       PROCEDURE helpKillIR;
  163.  
  164.          BEGIN
  165.             DisplayLine('\killir -- Attempts to remove IR from memory');
  166.          END;
  167.  
  168.       PROCEDURE cmdKillIR;
  169.  
  170.          VAR
  171.             mySRQRecord: srqGoAwayOut;
  172.             myErr      : OSErr;
  173.             IRID       : Integer;
  174.  
  175.          BEGIN
  176.             WriteCR;
  177.             SendRequest(srqGoAway,sendToName + stopAfterOne,@IRString,0,@mySRQRecord);
  178.             IF _toolErr = 0 THEN BEGIN
  179.                AcceptRequests(NIL,mySRQRecord.resultID,NIL);
  180.                IRID := UserShutDown(mySRQRecord.resultID,0);
  181.                DisplayErrorLine('IR''s user ID was $',IRID);
  182.             END
  183.             ELSE
  184.                DisplayErrorLine('Couldn''t kill IR -- error $',_toolErr);
  185.          END;
  186.  
  187.       PROCEDURE helpIRInfo;
  188.  
  189.          BEGIN
  190.             DisplayLine('\irinfo -- determines if IR is present');
  191.          END;
  192.  
  193.       PROCEDURE cmdIRInfo;
  194.  
  195.          VAR
  196.             myInfoRec  : IRInputRecord;
  197.                 IRVersion  : string[10];
  198.  
  199.          BEGIN
  200.             WriteCR;
  201.             SendRequest(askIRAreYouThere,sendToName + stopAfterOne,@IRString,0,@myInfoRec);
  202.             IF _toolErr = 0 THEN BEGIN
  203.                     VersionString(0,myInfoRec.version,@IRVersion);
  204.                DisplayLineNoCR('IR version ');
  205.                     DisplayLineNoCr(IRVersion);
  206.                     DisplayErrorLine(' is installed with user ID $',myInfoRec.userID);
  207.                 END
  208.             ELSE
  209.                DisplayLine('IR is not installed.');
  210.             WriteCR;           { extra blank line }
  211.          END;
  212.  
  213.       PROCEDURE helpGetIRPrefs;
  214.  
  215.          BEGIN
  216.             DisplayLine('\getirprefs -- Returns IR''s current preferences');
  217.          END;
  218.  
  219.       PROCEDURE cmdGetIRPrefs;
  220.  
  221.          VAR
  222.             myPrefs    : askGetPrefsOutputRecord;
  223.  
  224.          BEGIN
  225.             WriteCR;
  226.             SendRequest(askIRGetPrefs,sendToName + stopAfterOne,@IRString,0,@myPrefs);
  227.             IF _toolErr = 0 THEN BEGIN
  228.                DisplayLine('IR''s current preferences:');
  229.                IF BAND(myPrefs.preferences,irNoDuplicates) <> 0 THEN
  230.                   DisplayLine('    Duplicates not allowed');
  231.                IF BAND(myPrefs.preferences,irKillDuplicates) <> 0 THEN
  232.                   DisplayLine('    Duplicates must be removed');
  233.                IF BAND(myPrefs.preferences,irDontOpenNDAs) = 0 THEN
  234.                   DisplayLine('    Open new NDAs after installing');
  235.                IF BAND(myPrefs.preferences,irKillFinderExts) <> 0 THEN
  236.                   DisplayLine('    Remove Finder extensions when quitting Finder');
  237.                IF BAND(myPrefs.preferences,irCopyExistNDAs) = 0 THEN
  238.                   DisplayLine('    Open existing NDAs if possible instead of installing');
  239.                IF BAND(myPrefs.preferences,irWaitOpenFailed) <> 0 THEN
  240.                   DisplayLine(
  241.                           '    Respond to finderSaysOpenFailed instead of finderSaysBeforeOpen'
  242.                               );
  243.                IF BAND(myPrefs.preferences,irIgnoreProblems) <> 0 THEN
  244.                   DisplayLine('    Don''t inform user about problems');
  245.             END                { if _toolErr }
  246.             ELSE
  247.                DisplayErrorLine('Getting preferences failed -- error $',myPrefs.irError);
  248.             WriteCR;
  249.  
  250.          END;
  251.  
  252.       PROCEDURE helpSetIRPrefs;
  253.  
  254.          BEGIN
  255.             DisplayLine('xxxx\setirprefs -- sets IR''s current preferences');
  256.          END;
  257.  
  258.       PROCEDURE cmdSetIRPrefs;
  259.  
  260.          VAR
  261.             myPrefs    : askSetPrefsOutputRecord;
  262.  
  263.          BEGIN
  264.             d := NLService(@myRangeBuffer,nlGetRange);
  265.  
  266.             SendRequest(askIRSetPrefs,sendToName + stopAfterOne,@IRString,
  267.                         myRangeBuffer.rawStart,@myPrefs);
  268.             IF _toolErr <> 0 THEN
  269.                DisplayErrorLine('Setting preferences failed -- error $',myPrefs.irError);
  270.             WriteCR;
  271.  
  272.          END;
  273.  
  274.       PROCEDURE helpDoIRPrefs;
  275.  
  276.          BEGIN
  277.             DisplayLine('doirprefs -- performs the IR preferences dialog if possible');
  278.          END;
  279.  
  280.       PROCEDURE cmdDoIRPrefs;
  281.  
  282.          BEGIN
  283.             SendRequest(askIRDoPrefs,sendToName + stopAfterOne,@IRString,0,NIL);
  284.          END;
  285.  
  286.       PROCEDURE helpIRInstall;
  287.  
  288.          BEGIN
  289.             DisplayLine(
  290.              'xxxx\irinstall "pathname" -- ask IR to install pathname with optional flags xxxx'
  291.                         );
  292.          END;
  293.  
  294.       PROCEDURE cmdIRInstall;
  295.  
  296.          VAR
  297.             installInput: askInstallInputRecord;
  298.             installOutput: askInstallOutputRecord;
  299.             osKind     : Byte;
  300.  
  301.          BEGIN
  302.  
  303.             WriteCR;
  304.             osKind := LoWrd(NLService($E100BC,nlGetByte));
  305.             IF osKind = 1 THEN BEGIN
  306.  
  307.                d := NLService(@myRangeBuffer,nlGetRange);
  308.                d := NLService(@myEvalBuffer,nlEvalExpr);
  309.  
  310.                installInput.flags := myRangeBuffer.rawStart;
  311.                installInput.pathname := @myEvalBuffer.actExprSize;
  312.                installOutput.irError := 0;
  313.  
  314.                GFIBuffer.pCount := 4;
  315.                GFIBuffer.pathname := installInput.pathname;
  316.  
  317.                GetFileInfoGS(GFIBuffer);
  318.  
  319.                IF _toolErr = 0 THEN BEGIN
  320.                   installInput.filetype := GFIBuffer.filetype;
  321.                   installInput.auxtype := GFIBuffer.auxtype;
  322.  
  323.                   SendRequest(askIRToInstall,sendToName + stopAfterOne,@IRString,@installInput,
  324.                               @installOutput);
  325.  
  326.                   IF _toolErr <> 0 THEN
  327.                      IF installOutput.irError <> 0 THEN
  328.                         DisplayErrorLine('Installation failed -- IR returned result $',
  329.                                          installOutput.irError)
  330.                      ELSE
  331.                         DisplayErrorLine('Installation failed -- IR Module got error $',
  332.                                          _toolErr)
  333.                   ELSE
  334.                      DisplayErrorLine('Installation succesful -- the new file has user ID $',
  335.                                       installOutput.userID);
  336.  
  337.                END
  338.                ELSE
  339.                   DisplayErrorLine('GS/OS error $',_toolErr)
  340.  
  341.             END
  342.             ELSE
  343.                DisplayLine('IR can''t install if GS/OS isn''t active.');
  344.             WriteCR;
  345.          END;
  346.  
  347.       FUNCTION NDAOpen: ptr;
  348.  
  349.          BEGIN
  350.             { First entry is for the module itself }
  351.             moduleName := concat('IR Module ',ModuleVersion);
  352.             WITH myCommandTable[1] DO BEGIN
  353.                cmdName := @moduleName;
  354.                cmdProc := NIL;
  355.                helpProc := @helpModule; { displays command summary }
  356.             END;
  357.             commandKillIR := 'killir';
  358.             WITH myCommandTable[2] DO BEGIN
  359.                cmdName := @commandKillIR;
  360.                cmdProc := @cmdKillIR;
  361.                helpProc := @helpKillIR;
  362.             END;
  363.             commandIRInfo := 'irinfo';
  364.             WITH myCommandTable[3] DO BEGIN
  365.                cmdName := @commandIRInfo;
  366.                cmdProc := @cmdIRInfo;
  367.                helpProc := @helpIRInfo;
  368.             END;
  369.             commandGetIRPrefs := 'getirprefs';
  370.             WITH myCommandTable[4] DO BEGIN
  371.                cmdName := @commandGetIRPrefs;
  372.                cmdProc := @cmdGetIRPrefs;
  373.                helpProc := @helpGetIRPrefs;
  374.             END;
  375.             commandSetIRPrefs := 'setirprefs';
  376.             WITH myCommandTable[5] DO BEGIN
  377.                cmdName := @commandSetIRPrefs;
  378.                cmdProc := @cmdSetIRPrefs;
  379.                helpProc := @helpSetIRPrefs;
  380.             END;
  381.             commandDoIRPrefs := 'doirprefs';
  382.             WITH myCommandTable[6] DO BEGIN
  383.                cmdName := @commandDoIRPrefs;
  384.                cmdProc := @cmdDoIRPrefs;
  385.                helpProc := @helpDoIRPrefs;
  386.             END;
  387.             commandIRInstall := 'irinstall';
  388.             WITH myCommandTable[7] DO BEGIN
  389.                cmdName := @commandIRInstall;
  390.                cmdProc := @cmdIRInstall;
  391.                helpProc := @helpIRInstall;
  392.             END;
  393.             { Last entry terminates list by having NIL for command name }
  394.             WITH myCommandTable[8] DO BEGIN
  395.                cmdName := NIL;
  396.             END;
  397.  
  398.             WITH InfoRec DO BEGIN
  399.                size := sizeof(InfoRec);
  400.                format := 0;
  401.                patchType := 0;
  402.                patch := @NLService;
  403.                bytesPerCommand := 12;
  404.                cmdTable := @myCommandTable[1];
  405.             END;
  406.             myEvalBuffer.maxExprSize := sizeof(myEvalBuffer);
  407.             myEvalBuffer.actExprSize := 0;
  408.             IRString := 'Apple~IR~';
  409.             NDAOpen := @InfoRec;
  410.          END;
  411.  
  412.       PROCEDURE NDAAction(Code: Integer; Param: Longint);
  413.  
  414.          VAR
  415.  
  416.             resApp, resFile    : Integer;
  417.             versHandle : Handle;
  418.  
  419.          BEGIN
  420.             CASE Code OF
  421.                actBirth: BEGIN
  422.                         resApp := GetCurResourceApp;
  423.                         ResourceStartUp(MMStartUp);
  424.                   resFile := OpenResourceFile(1,NIL,GSString255Ptr(LGetPathname2(MMStartUp,
  425.                              1)));
  426.                   IF _toolErr = 0 THEN BEGIN
  427.                      versHandle := LoadResource(rVersion,1);
  428.                      IF ((_toolErr = 0) and (QDVersion >= $0307)) THEN BEGIN
  429.                         HLock(versHandle); { not locked in resource file because the Finder
  430.                                             uses this, too }
  431.                         VersionString(0,NLService(versHandle^,nlGetLong),
  432.                                       @ModuleVersion);
  433.                      END;      { _toolErr/QDVersion from LoadResource }
  434.                   END;         { _toolErr from OpenResourceFile }
  435.                   ResourceShutDown; { closes our files for us }
  436.                         SetCurResourceApp(resApp);
  437.                END;            { actBirth }
  438.             END;               { case }
  439.          END;                  { NDAAction }
  440. END.
  441.